package com.iot.service;

import com.iot.mapper.*;
import com.iot.pojo.MRef;
import com.iot.pojo.MTmenu;
import com.iot.pojo.MTreeNode;
import com.iot.pojo.MTusers;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TusersServiceImpl implements TusersService {
    @Autowired
    private TusersMapper mapper;
    @Autowired
    private DTusersMapper dTusersMapper;
    @Autowired
    private TmenuMapper tmenuMapper;
    @Autowired
    private DTmenuMapper dtmenuMapper;
    @Autowired
    private DTreeMapper dTreeMapper;
    @Autowired
    private DTusersMapper dmapper;
    @Autowired
    private DRefMapper dRefMapper;

    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }

    @Override
    public int insert(MTusers mTusers) throws Exception {
        int count = 0;
        count = getRecordCount("  AND SNAME='" + mTusers.getSNAME() + "'");
        if (count > 0)
            throw new Exception("该名称已存在");
        mTusers.setPASSWORD("123");
        return mapper.insert(mTusers);
    }

    @Override
    public void update(MTusers mTusers) throws Exception {
        int count = 0;
        count = getRecordCount(" AND SNAME='" + mTusers.getSNAME() + "' AND USERS_ID!=" + mTusers.getUSERS_ID() + "");
        if (count > 0)
            throw new Exception("该名称已存在");
        mapper.update(mTusers);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTusers single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTusers> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public MTusers login(String username, String password) throws Exception {
        if (Common.isEmpty(username)) {
            throw new Exception("用户名称不能为空！");
        }
        MTusers mTusers = dTusersMapper.getUserByName(username);
        if (mTusers != null) {
            if (mTusers.getBDEL() == 0) {
                throw new Exception("用户已经被禁止登录系统！");
            } else if (!mTusers.getPASSWORD().equals(password)) {
                throw new Exception("密码不正确！");
            }
            return mTusers;
        } else {
            throw new Exception("用户名不正确！");
        }

    }

    public List<MTmenu> getMenuByUserId(int userId) throws Exception {
        MTusers mTusers = mapper.single(userId);
        if (mTusers != null) {
            if (mTusers.getSNAME().equals("admin") || mTusers.getSNAME().equals("system")) {
                List<MTmenu> list = tmenuMapper.fetch(" AND BDEL=1 ORDER BY MENU_NO, SORDER");
                return list;
            } else if (mTusers.getBADMIN() == 1) {
                List<MTmenu> list = tmenuMapper.fetch(" AND BDEL=1 AND BADMIN2=1 ORDER BY MENU_NO, SORDER");
                return list;

            } else {
//                List<Map<String, String>> map = dtmenuMapper.getAuthorityByUserId(userId);
                List<MTmenu> list = tmenuMapper.fetch(" AND BDEL=1 AND MENU_ID IN (SELECT MENU_ID FROM TROLE_AUTHORITY WHERE ROLE_ID IN(SELECT ROLE_ID FROM TUSERS WHERE USERS_ID=" + userId + ")) ORDER BY MENU_NO, SORDER");
                return list;
            }
        } else {
            throw new Exception("没有找到登录用户信息，请与管理员联系！");
        }

    }

    @Override
    public List<Map<String, Object>> fetchByPage(Map<String, Object> map) {

        return dmapper.fetchByPage(map);
    }


    @Override
    public Map<String, Object> initList(int appId) {
        Map<String, Object> map = new HashMap<>();
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public Map<String, Object> initEdit(int appId) {
        List<MTreeNode> deptList = dTreeMapper.getDept(appId);
        Map<String, Object> map = new HashMap<>();
        List<MRef> roleList = dRefMapper.getRole(appId);
        map.put("role", roleList);
        map.put("dept", deptList);
        return map;
    }

    @Override
    public Map<String, Object> getUserById(int userId) {
        return dmapper.getUserById(userId);
    }

    public int getUserByEmpId(int empId) {
        return getRecordCount(" AND EMP_ID='" + empId + "'");
    }

    @Override
    public void changePwd(int userId, String oldPwd, String newPwd) throws Exception {
        MTusers mTusers = mapper.single(userId);
        if (mTusers != null) {
            if (!mTusers.getPASSWORD().equals(oldPwd)) {
                throw new Exception("旧密码错误");
            } else {
                MTusers m2 = new MTusers();
                m2.setUSERS_ID(userId);
                m2.setPASSWORD(newPwd);
                mapper.update(m2);
            }
        } else {
            throw new Exception("用户信息错误");
        }
    }


}
